package cn.memorynine.consumer.service;

import cn.hutool.core.bean.BeanUtil;
import cn.memorynine.dao.entity.Activity;
import cn.memorynine.dao.entity.ActivityConsumerParticipation;
import cn.memorynine.dao.entity.ActivityState;
import cn.memorynine.dao.entity.ShopCommodity;
import cn.memorynine.dao.mapper.ActivityConsumerParticipationMapper;
import cn.memorynine.dao.mapper.ActivityMapper;
import cn.memorynine.dao.mapper.ActivityStateMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import cn.memorynine.common.model.ResponseWrapper;
import cn.memorynine.consumer.model.activity.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* 用户前台-活动服务
* @author 代码生成器
*/
@Slf4j
@Service
public class ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityConsumerParticipationMapper activityConsumerParticipationMapper;

    @Autowired
    private ActivityStateMapper activityStateMapper;
    /**
     * 1.获取活动列表
     * @return
     */
    public ResponseWrapper activityList(ActivityListParam param){
        ActivityListResult activityListResult = new ActivityListResult();
        Page<Activity> pageInfo = new Page<>(param.getPageNum(), param.getNum(),false);
        List<ActivityList> activityLists = new ArrayList<>();
        if (param.getStateId() == -1) {
            //获取活动总数
            activityListResult.setTotalNum(activityMapper.selectCount(
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
            ));
            Page<Activity> activityPage = activityMapper.selectPage(pageInfo,
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
                            .orderByAsc(Activity::getStateId)
            );
            List<Activity> activityList = activityPage.getRecords();
            activityList.forEach(item->{
                ActivityList activityListItem = new ActivityList();
                BeanUtil.copyProperties(item,activityListItem);
                activityLists.add(activityListItem);
            });
        } else {
            //获取活动总数
            activityListResult.setTotalNum(activityMapper.selectCount(
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
                            .eq(Activity::getStateId,param.getStateId())
            ));
            Page<Activity> activityPage = activityMapper.selectPage(pageInfo,
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
                            .eq(Activity::getStateId,param.getStateId())
                            .orderByAsc(Activity::getStateId)
            );
            List<Activity> activityList = activityPage.getRecords();
            activityList.forEach(item->{
                ActivityList activityListItem = new ActivityList();
                BeanUtil.copyProperties(item,activityListItem);
                activityLists.add(activityListItem);
            });
        }

        activityListResult.setActivityList(activityLists);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(activityListResult);
    }

    /**
     * 2.查看活动详情
     * @return
     */
    public ResponseWrapper activityInfo(ActivityInfoParam param){
        ActivityInfoResult activityInfoResult = new ActivityInfoResult();
        List<Activity> activityList = activityMapper.selectList(
                Wrappers.<Activity>lambdaQuery().eq(Activity::getAid, param.getAid())
        );

        if (!activityList.isEmpty()){
            Activity activity = activityList.get(0);
            BeanUtil.copyProperties(activity,activityInfoResult);
            String name = activityStateMapper.selectOne(
                    Wrappers.<ActivityState>lambdaQuery()
                            .eq(ActivityState::getId,activity.getStateId())
            ).getName();
            activityInfoResult.setStateName(name);
            //查看用户是否参与了本活动
            List<ActivityConsumerParticipation> activityConsumerParticipations = activityConsumerParticipationMapper.selectList(
                    Wrappers.<ActivityConsumerParticipation>lambdaQuery()
                            .eq(ActivityConsumerParticipation::getConsumerAid, param.getTokenConsumerAid())
                            .eq(ActivityConsumerParticipation::getActivityAid, activity.getAid())
            );
            if (!activityConsumerParticipations.isEmpty()){
                activityInfoResult.setIsJoin(true);
            } else {
                activityInfoResult.setIsJoin(false);
            }
        }

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(activityInfoResult);
    }

    /**
     * 3.参加/退出活动
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper attendActivity(AttendActivityParam param){
        //1.不管参没参加过，直接删他丫的
        activityConsumerParticipationMapper.delete(
                Wrappers.<ActivityConsumerParticipation>lambdaQuery()
                        .eq(ActivityConsumerParticipation::getConsumerAid, param.getTokenConsumerAid())
                        .eq(ActivityConsumerParticipation::getActivityAid, param.getAid())
        );
        ActivityConsumerParticipation activityConsumerParticipation = new ActivityConsumerParticipation();
        activityConsumerParticipation.setActivityAid(param.getAid());
        activityConsumerParticipation.setConsumerAid(param.getTokenConsumerAid());
        if (param.getJoin()){
            activityConsumerParticipationMapper.insert(activityConsumerParticipation);
        } else {
            activityConsumerParticipationMapper.delete(
                    Wrappers.<ActivityConsumerParticipation>lambdaQuery()
                            .eq(ActivityConsumerParticipation::getActivityAid,param.getAid())
                            .eq(ActivityConsumerParticipation::getConsumerAid,param.getTokenConsumerAid())
            );
        }

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 4.活动状态列表
     */
    public ResponseWrapper activityStateList(ActivityStateListParam param) {
        List<ActivityStateListResult> activityStateListResults = new ArrayList<>();
        activityStateListResults.add(new ActivityStateListResult(-1,"全部状态"));
        List<ActivityState> activityStates = activityStateMapper.selectList(null);

        activityStates.forEach(item->{
            ActivityStateListResult activityStateListResult = new ActivityStateListResult();
            BeanUtil.copyProperties(item,activityStateListResult);
            activityStateListResults.add(activityStateListResult);
        });
        return ResponseWrapper.markSuccess(activityStateListResults);
    }
}